﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
/*!
 * @file
 * @brief  MessageLoggerHelper类说明：管道消息记录成日志文件助手
 * @verbatim
    VERSION DATE                    BY                       CHANGE/COMMENT
  ----------------------------------------------------------------------------
    V0.0.1   2021/6/24             Inossem                   新建

    COPYRIGHT Inossem 2021
  ----------------------------------------------------------------------------
 * @endverbatim
 */

using DWF.Studio.ScriptEngine.Event;
using DWF.Studio.ScriptEngine.IPC.Msg;
using DWF.Studio.Share.Log;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static DWF.Studio.ScriptEngine.Custom.ExecutionContext;

namespace DWF.Studio.ScriptEngine.Custom
{
    public class MessageLoggerHelper
    {
        /// <summary>
        /// 记录日志
        /// </summary>
        /// <returns>是否为拦截消息</returns>
        internal static bool Log<T>(
            T datum
            , EMsgType msgType
            , StartupProperties startupProps
            )
        {
            //判断必要的参数
            if (
                datum == null
                || string.IsNullOrEmpty(startupProps.projectName)
                || string.IsNullOrEmpty(startupProps.scriptFileName_WithoutExtension)
                )
            {
                return false;
            }

            //消息记录到文件还是发送判断 Oliver add 2021-06-18
            bool msgToFileEMsgType = (
                                msgType.Equals(EMsgType.ExcuteStart)

                                || msgType.Equals(EMsgType.ExcuteTrack)
                                || msgType.Equals(EMsgType.ExcuteUnhandledException)

                                || msgType.Equals(EMsgType.LoadSubEngine)
                                || msgType.Equals(EMsgType.UnloadSubEngineEcho)

                                || msgType.Equals(EMsgType.ExcuteEnd)
                             );

            //是否是拦截消息
            bool isFilterMsg = (
                                    msgType.Equals(EMsgType.ExcuteTrack)
                                    || msgType.Equals(EMsgType.UnloadSubEngineEcho)  //拦截卸载子引擎回声数据包，这个数据包用以记录子引擎已卸载消息的回传
                               );


            //静默执行，直接退出 Oliver add 2021-06-25
            if (startupProps.IsSilentExecution) return isFilterMsg;


            if (msgToFileEMsgType)
            {
                if (startupProps.IsLogToFile)
                {
                    switch (msgType)
                    {
                        case EMsgType.ExcuteUnhandledException:
                            LogHelper.Logger.LogMsg(
                                startupProps.projectName
                                , startupProps.scriptFileName_WithoutExtension
                                , $"[{DateTime.Now:O}] [Error]{System.Environment.NewLine}" + (datum as ExecuteUnhandledExceptionEventArgs)?.ToString()
                            );
                            break;
                        case EMsgType.ExcuteTrack:
                            LogHelper.Logger.LogMsg(
                                startupProps.projectName
                                , startupProps.scriptFileName_WithoutExtension
                                , $"[{DateTime.Now:O}] " + (datum as ExecuteTrackEventArgs)?.TrackData
                            );
                            break;
                        case EMsgType.ExcuteStart:
                            // 记录日志 - 开始
                            LogHelper.Logger.LogMsg(
                                startupProps.projectName
                                , startupProps.scriptFileName_WithoutExtension
                                , $"/*** {(datum as ExecuteStartEventArgs)?.Message} [{DateTime.Now:O}]*********/"
                            );
                            break;
                        case EMsgType.ExcuteEnd:
                            // 记录日志 - 完成
                            LogHelper.Logger.LogMsg(
                                startupProps.projectName
                                , startupProps.scriptFileName_WithoutExtension
                                , $"/*** End ExecuteScript [{DateTime.Now.ToString("G")}]*********/"
                            );
                            break;
                        case EMsgType.LoadSubEngine:
                            LogHelper.Logger.LogMsg(
                                startupProps.projectName
                                , startupProps.scriptFileName_WithoutExtension
                                , $"[{ DateTime.Now:O}] [Load Engine] [{(datum as LoadSubEngineEventArgs)?.SubEngineId}] [{(datum as LoadSubEngineEventArgs)?.ScriptPath}]"
                            );
                            break;
                        case EMsgType.UnloadSubEngineEcho:
                            LogHelper.Logger.LogMsg(
                                startupProps.projectName
                                , startupProps.scriptFileName_WithoutExtension
                                , $"[{ DateTime.Now:O}] [Unload Engine] [{(datum as UnLoadSubEngineEventArgs)?.SubEngineId}]"
                            );
                            break;
                        default:
                            break;
                    }

                    if (isFilterMsg) return isFilterMsg;
                }
                if (startupProps.IsConsoleOutToFile)
                {
                    switch (msgType)
                    {
                        case EMsgType.ExcuteStart:
                            // 记录控制台 - 开始
                            LogHelper.Logger.LogMsg(
                                startupProps.projectName
                                , startupProps.scriptFileName_WithoutExtension
                                , $"/*** {(datum as ExecuteStartEventArgs)?.Message} [{DateTime.Now:O}]*********/"
                                , "_ConsoleOut"
                            );
                            break;
                        case EMsgType.ExcuteEnd:
                            // 记录控制台 - 完成
                            LogHelper.Logger.LogMsg(
                                startupProps.projectName
                                , startupProps.scriptFileName_WithoutExtension
                                , $"/*** End ExecuteScript [{DateTime.Now:O}]*********/"
                                , "_ConsoleOut"
                            );
                            break;
                        default:
                            break;
                    }

                    return false;
                }
            }

            return false;
        }
    }
}